package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mysql.cj.util.StringUtils;
import org.example.domain.dto.Condition;
import org.example.domain.dto.OrderStatus;
import org.example.domain.entity.Order;
import org.example.domain.entity.OrderItem;
import org.example.domain.model.OrdersPage;
import org.example.mapper.OrderItemMapper;
import org.example.mapper.OrderMapper;
import org.example.service.IOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements IOrderService {

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private OrderItemMapper orderItemMapper;

    public OrderServiceImpl(OrderMapper orderMapper) {
        this.orderMapper = orderMapper;
    }

    @Override
    public OrdersPage finOrder(Condition condition) {

        Page<Order> orders = PageHelper.startPage(condition.getPage(), condition.getLimit());
        orderMapper.findOrder(condition);
        OrdersPage ordersPage = new OrdersPage();
        ordersPage.setOrders(orders);
        ordersPage.setTotal(orders.getTotal());
        return ordersPage;
    }

    @Override
    public List<Order> queryById(OrderStatus orderStatus) {
        return orderMapper.queryOrder(orderStatus.getUserId(), orderStatus.getStatus());
    }

    @Override
    public List<List<OrderItem>> queryByOrderId(List<String> orderId) {
        List<List<OrderItem>> list = new ArrayList<>();
        for (String s : orderId) {

            List<OrderItem> orderItem = orderItemMapper.selectByOrderId(s);
            list.add(orderItem);
        }
        return list;
    }

    @Override
    public void updateStatus(String orderId) {
        orderMapper.updateStatus(orderId);
    }

    @Override
    public Order queryByOne(String orderId){
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Order order = orderMapper.selectById(orderId);
        String format = df.format(new Date(order.getCreateTime().getTime() + 15 * 1000));
        int timeFlag = format.compareTo(new Date().toString());

        return order;
    }

    //根据用户id查询
    @Override
    public List<List<OrderItem>> queryByUserId(Integer userId) {
        List<String> list = orderMapper.queryOrderByUserId(userId);
        List<List<OrderItem>> lists = queryByOrderId(list);
        return lists;
    }
}
